package com.umbrella.worker.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.umbrella.worker.dao.WOperateMapper;
import com.umbrella.worker.dao.WRoleMapper;
import com.umbrella.worker.dao.WRoleOperateMapper;
import com.umbrella.worker.dto.OperateDO;
import com.umbrella.worker.dto.RoleDO;
import com.umbrella.worker.entity.WOperate;
import com.umbrella.worker.entity.WRole;
import com.umbrella.worker.entity.WRoleExample;
import com.umbrella.worker.entity.WRoleOperate;
import com.umbrella.worker.entity.WRoleOperateExample;
import com.umbrella.worker.query.RoleQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IRoleService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements IRoleService {

private static Logger logger = Logger.getLogger(OperateServiceImpl.class);
	@Autowired
	private WRoleMapper roleMapper;
	@Autowired
	private WOperateMapper operateMapper;
	@Autowired
	private WRoleOperateMapper roleOperateMapper;

	@Override
	public ResultDO create(RoleDO roleDO) {
		
		WRole role = new WRole();
		
		ResultSupport result = BeanUtilsExtends.copy(role, roleDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		
		role.setDatalevel(1);
		role.setStatus(1);
		role.setCreateTime(Calendar.getInstance().getTime());
		role.setModifiTime(Calendar.getInstance().getTime());
		
		try {
			recordNum = roleMapper.insertSelective(role);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:contact][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		for(Integer opId : roleDO.getOpids()) {
			WRoleOperate roleOperate = new WRoleOperate();
			roleOperate.setwRoleId(role.getId());
			roleOperate.setwOperateId(opId);
			try {
				roleOperateMapper.insertSelective(roleOperate);
			} catch (Exception e) {
				result.setSuccess(false);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				logger.error("[obj:contact][opt:create][msg:" + e.getMessage()
				+ "]");
				return result;
			}
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, role.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public ResultDO modifi(RoleDO roleDO) {
		
		WRole role = new WRole();

		ResultSupport result = BeanUtilsExtends.copy(role, roleDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		role.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = roleMapper.updateByPrimaryKeySelective(role);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:contact][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO remove(int id) {
		ResultSupport result = new ResultSupport();
		
		WRole role = new WRole();
		
		if(StringUtil.isGreatOne(id)) {
			role.setId(id);
		} else {
		    result.setSuccess(false);
		    return result;
		}
		role.setDatalevel(-1);
		int recordNum = -1;
		try {
			recordNum = roleMapper.updateByPrimaryKeySelective(role);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:contact][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		
	
		WRoleOperateExample example = new WRoleOperateExample();
		example.createCriteria().andWRoleIdEqualTo(id);
	
		recordNum = -1;
		try {
			recordNum = roleOperateMapper.deleteByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:contact][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		if (recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public ResultDO get(int id) {
		
		ResultSupport result = new ResultSupport();
		
		WRole role = null;
		if(!StringUtil.isGreatOne(id)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			role = roleMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<WOperate> list = null;
		
		try {
			list = operateMapper.selectOperateInRole(id);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OperateDO> operates = null;
		if(list != null) {
			operates = getOperateDOList(list);
		}
		
		RoleDO roleDO = getRoleDO(role);
		
		if(operates != null) {
			roleDO.setOperates(operates);
		}
		
		if(roleDO != null) {
			result.setModel(ResultSupport.FIRST_MODEL_KEY, roleDO);
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}
		
		return result;
	}

	@Override
	public ResultDO list(RoleQuery roleQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WRoleExample example = new WRoleExample();
		WRoleExample.Criteria c = example.createCriteria();
		
		if(StringUtil.isGreatOne(roleQuery.getwRType())) {
			c.andWRTypeEqualTo(roleQuery.getwRType());
		}

		c.andDatalevelEqualTo(1);
	
		List<WRole> list = null;
		
		try {
			list = roleMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<RoleDO> rolelist = getRoleDOList(list);
			
		result.setModel(ResultSupport.FIRST_MODEL_KEY, rolelist);
		
		return result;
	}

}
